package cn.edu.xaut.mybatisers.mall.Controller;

import cn.edu.xaut.mybatisers.mall.Entity.*;
import cn.edu.xaut.mybatisers.mall.Exception.ExceptionEntity.LoginException;
import cn.edu.xaut.mybatisers.mall.Exception.ExceptionEntity.ParametersIncompleteException;
import cn.edu.xaut.mybatisers.mall.ServiceImp.AdminServiceImp;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.sql.SQLException;

@Controller
public class AdminController {
    @Autowired
    AdminServiceImp adminServiceImp;

    @ApiOperation("管理员登录")
    @RequestMapping(value = "/admin/login", method = RequestMethod.POST)
    @ResponseBody
    public Admin login(Admin admin, String verificationCode,HttpServletResponse response, HttpSession session) throws ParametersIncompleteException, LoginException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, SQLException {
        if (admin.getUsername() == null || admin.getPassword() == null){
            throw new ParametersIncompleteException((admin.getUsername()==null)?"Account Member":"password");
        }
        return  adminServiceImp.login(admin,verificationCode, response, session);
    }

    @ApiOperation("管理员修改订单")
    @RequestMapping(value = "/admin/alterBill", method = RequestMethod.POST)
    @ResponseBody
    public Boolean alterBill(@RequestBody Bill bill) throws ParametersIncompleteException, SQLException {
        if (bill.getOrder_number() == null){
            throw new ParametersIncompleteException("order number");
        }
        if (bill.getBillStatus()!= 0 && !bill.statusMapper.containsKey(bill.getBillStatus())) {
            throw new ParametersIncompleteException("订单状态");
        }
        return adminServiceImp.alterBill(bill);
    }

    @ApiOperation("管理员条件查询订单")
    @RequestMapping(value = "/admin/findbills", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo<Bill> findBill(@RequestBody JSONObject requestParam) throws SQLException {
        Bill bill = new Bill();
        bill.setOrder_number( requestParam.has("order_number")? (String) requestParam.get("order_number") :null);
        bill.setBillStatus( requestParam.has("billStatus")? Integer.valueOf((String) requestParam.get("billStatus")) : null);
        bill.setTeleNumber( requestParam.has("teleNumber")? (String) requestParam.get("teleNumber") :null);
        return adminServiceImp.findBills(bill,
                requestParam.has("accountMember") ? (String) requestParam.get("accountMember") : null,
                requestParam.has("pageNum") ? (int) requestParam.get("pageNum") : 1,
                requestParam.has("pageSize") ? (int) requestParam.get("pageSize") : 5
        );
    }


    @ApiOperation("添加商品")
    @RequestMapping(value = "/admin/addCommodity", method = RequestMethod.POST)
    @ResponseBody
    public AReturnDataStruct addCommodity(Commodity commodity, HttpSession session) throws SQLException, IOException {
        adminServiceImp.addCommodity(commodity, (Integer) session.getAttribute("admin_id"));
        AReturnDataStruct return_data = new AReturnDataStruct();
        return_data.setResult(true);
        return_data.setCode(200);
        return_data.setData("添加成功");
        return return_data;
    }


    @ApiOperation("删除商品")
    @RequestMapping(value = "/admin/deleteCommodity", method = RequestMethod.DELETE)
    @ResponseBody
    public AReturnDataStruct deleteCommodity(@RequestBody Commodity commodity) throws ParametersIncompleteException, SQLException {
        if (commodity.getId() <= 0){
            throw new ParametersIncompleteException("商品id不完整");
        }
        AReturnDataStruct return_data = new AReturnDataStruct();
        if (adminServiceImp.deleteCommodity(commodity) > 0){
            return_data.setResult(true);
            return_data.setCode(200);
            return_data.setMessage("删除成功");
        }else {
            return_data.setResult(false);
            return_data.setCode(200);
            return_data.setMessage("没有该商品");
        }
        return return_data;
    }

    @ApiOperation("修改商品")
    @RequestMapping(value = "/admin/modifyCommodity", method = RequestMethod.PUT)
    @ResponseBody
    public AReturnDataStruct modifyCommodity(@RequestBody Commodity commodity) throws ParametersIncompleteException, SQLException {
        if (commodity.getId() <= 0) {
            throw new ParametersIncompleteException("商品id不完整");
        }
        Commodity return_commodity = adminServiceImp.alterCommodity(commodity);
        AReturnDataStruct return_data = new AReturnDataStruct();
        return_data.setData(return_commodity);
        return_data.setResult(true);
        return_data.setCode(200);
        return_data.setMessage( return_commodity != null ?"修改成功" : "没有该商品");
        return return_data;
    }

    @ApiOperation("条件查询商品")
    @RequestMapping(value = "/findCommodity", method = RequestMethod.GET)
    @ResponseBody
    public AReturnDataStruct findCommodity(
            Commodity commodity,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize) throws SQLException, IOException {
        PageInfo<Commodity> data = adminServiceImp.findCommodity(commodity, pageNum, pageSize);
        AReturnDataStruct return_data = new AReturnDataStruct();
        return_data.setResult(true);
        return_data.setCode(200);
        return_data.setMessage("查询成功");
        return_data.setData(data);
        return return_data;
    }

    @ApiOperation("添加商品类型")
    @RequestMapping(value = "/admin/addCommodityCategory", method = RequestMethod.POST)
    @ResponseBody
    public AReturnDataStruct addCommodityCategory(@RequestBody CommodityCategory commodityCategory) throws ParametersIncompleteException, SQLException {
        if (commodityCategory.getCategory() == null)
            throw new ParametersIncompleteException("category不完整");
        adminServiceImp.addCommodityCategory(commodityCategory);
        AReturnDataStruct return_data = new AReturnDataStruct();
        return_data.setResult(true);
        return_data.setCode(200);
        return_data.setMessage("添加成功");
        return return_data;
    }
    @ApiOperation("修改商品类型名称")
    @RequestMapping(value = "/admin/alterCommodityCategory", method = RequestMethod.PUT)
    @ResponseBody
    public AReturnDataStruct alterCommodityCategory(@RequestBody CommodityCategory commodityCategory) throws ParametersIncompleteException, SQLException {
        if (commodityCategory.getId() <= 0){
            throw new ParametersIncompleteException("categoryID不完整");
        }
        if (commodityCategory.getCategory() == null){
            throw new ParametersIncompleteException("category不完整");
        }
        CommodityCategory category = adminServiceImp.alterCommodityCategory(commodityCategory);
        AReturnDataStruct return_data = new AReturnDataStruct();
        return_data.setData(category);
        return_data.setResult(true);
        return_data.setCode(200);
        return_data.setMessage(category != null? "修改成功": "修改失败[没有给种类]");
        return return_data;
    }

    @ApiOperation("查询全部商品类型")
    @RequestMapping(value = "/findCommodityCategory", method = RequestMethod.GET)
    @ResponseBody
    public AReturnDataStruct findCommodityCategory() throws SQLException {
        AReturnDataStruct return_data = new AReturnDataStruct();
        return_data.setData(adminServiceImp.findCommodityCategory());
        return_data.setResult(true);
        return_data.setCode(200);
        return_data.setMessage("查询成功");
        return return_data;
    }

    @ApiOperation("删除商品类型")
    @RequestMapping(value = "/admin/deleteCommodityCategory", method = RequestMethod.DELETE)
    @ResponseBody
    public AReturnDataStruct deleteCommodityCategory(@RequestBody CommodityCategory commodityCategory) throws ParametersIncompleteException, SQLException {
        if (commodityCategory.getId() <= 0){
            throw new ParametersIncompleteException("categoryID不完整");
        }
        AReturnDataStruct return_data = new AReturnDataStruct();
        return_data.setResult(true);
        return_data.setCode(200);
        return_data.setMessage(adminServiceImp.deleteCommodityCategory(commodityCategory)>0 ? "添加成功":"删除失败[没有该种类]");
        return return_data;
    }
}
